﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using TuKu.Load;


namespace TuKu
{

    public partial class TopicAdd : Form
    {
        public TopicAdd()
        {
            InitializeComponent();
        }

        private void combGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            int groupid = int.Parse(new BLL.Group().GetModel(combGroup.Text).ID.ToString());
            DataBindClass.subGroupBind(groupid, combsubGroup);
            dataGridViewArray.Items.Clear();
        }
        private void TopicAdd_Load(object sender, EventArgs e)
        {
            DataBindClass.GroupBind(combGroup);
            combGroup.SelectedIndex = 0;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string small = txtSmall.Text.Trim();
            string big = txtBig.Text.Trim();
            string title = txtTitle.Text.Trim();
            if (!String.IsNullOrEmpty(small) && !String.IsNullOrEmpty(big) && !String.IsNullOrEmpty(title) && !String.IsNullOrEmpty(combsubGroup.Text))
            {
                BLL.Topic bll = new BLL.Topic();
                Model.Topic model = new global::TuKu.Model.Topic();
                model.AddTime = DateTime.Now;
                model.Body = big;
                model.ClickCount = 0;
                model.IsTop = 0;
                model.Small = small;
                model.subGroupID = int.Parse(new BLL.subGroup().GetModel(combsubGroup.Text).ID.ToString());
                model.Title = title;
                bll.Add(model);
                txtSmall.Text = string.Empty;
                txtBig.Text = string.Empty;
                txtTitle.Text = string.Empty;
                MessageBox.Show("添加成功");
            }
            else
            {
                MessageBox.Show("分类或URL列表不能为空");
            }


        }
        private void dataGridViewArray_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.All;
            }

        }
        private void dataGridViewArray_DragDrop(object sender, DragEventArgs e)
        {
            string[] MyFiles;
            MyFiles = (string[])(e.Data.GetData(DataFormats.FileDrop));
            // 循环处理数组并将文件添加到列表中。
            foreach (string str in MyFiles)
            {
                if (Common.FileManager.checkImgFileExtends(Common.FileManager.getFileExtends(str)))
                {
                    if (!dataGridViewArray.Items.Contains(str))
                    {
                        dataGridViewArray.Items.Add(str);
                    }
                }
            }
            //   dataGridViewArray.Items.AddRange(MyFiles);

        }
        private void btmAddList_Click(object sender, EventArgs e)
        {
            string groupName = combsubGroup.Text;
            if (!String.IsNullOrEmpty(groupName))
            {
                int groupId = int.Parse(new BLL.subGroup().GetModel(groupName).ID.ToString());
                BLL.Topic bll = new global::TuKu.BLL.Topic();
                Model.Topic model = new global::TuKu.Model.Topic();
                LoadingHandler.Show(this, LoadingStyle.Progress, args =>
                {
                    args.MaxValue = dataGridViewArray.Items.Count;
                    foreach (string str in dataGridViewArray.Items)
                    {
                        args.Execute(ex =>
                         {
                             //添加部份
                             model.AddTime = DateTime.Now;
                             model.Body = str;
                             model.ClickCount = 0;
                             model.IsTop = 0;
                             model.Small = str;
                             model.subGroupID = groupId;
                             model.Title = groupName;
                             if (!bll.Exist(model))
                             {
                                 bll.Add(model);
                             }
                             //
                         });
                        args.Value++;
                        args.Change();
                        System.Threading.Thread.Sleep(50);

                    }

                });
                dataGridViewArray.Items.Clear();
                combsubGroup.Focus();
            }
            else
            {
                MessageBox.Show("请选择子类");

            }
        }

        private void combsubGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridViewArray.Items.Clear();
            DataTable dt = new BLL.Topic().GetList(" subGroupId=" + new BLL.subGroup().GetModel(combsubGroup.SelectedItem.ToString()).ID.ToString()).Tables[0];
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    dataGridViewArray.Items.Add(dr["Body"].ToString());
                }
            }




        }

        private void btnSort_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string type = btn.Text;
            if (type == "时间")
            {
                type = " addtime ";
            }
            else
            {
                type = " pin ";
            }
            int groupid = int.Parse(new BLL.Group().GetModel(combGroup.Text).ID.ToString());
            DataBindClass.subGroupBind(groupid, combsubGroup,type);
            dataGridViewArray.Items.Clear();
        }
    }
}
